
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdio.h>

#include "usart.h"
/*UART*/

/*****************************************************************************
Name:          SIGNAL
Description:    USART RX interrupt
            INTERRUPT can be interrupted
            SIGNAL can't be interrupted
            (USART_RXC_vect)      
            (SIG_UART_RECV)
Parameters:    SIG_UART_RECV
Returns:        SIGNAL
*****************************************************************************/
ISR (USART_RXC_vect)
{ 
  data = UDR;
  
  
  USART_Transmit(data);

}


/*****************************************************************************
Name:          USART_Transmit_s
Description:    Transmits a string over USART
Parameters:    *s
Returns:        none
*****************************************************************************/
void USART_Transmit_s( char *s )
{
  //  loop until *s != NULL
  while (*s)
      {
        USART_Transmit(*s);
        s++;
      }
}


/*****************************************************************************
Name:          USART_Receive
Description:    Receive character from USART
Parameters:    void
Returns:        UDR
*****************************************************************************/
unsigned char USART_Receive( void )
{
  /* Wait for data to be received */
  while ( !(UCSRA & (1<<RXC)) )
      { };
  /* Get and return received data from buffer */
  return UDR;
}


/*****************************************************************************
Name:          USART_Transmit
Description:    Transmits a character over USART
Parameters:    data
Returns:        none
*****************************************************************************/
void USART_Transmit( unsigned char data )
{
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) );
    
  /* Put data into buffer, sends the data */
  UDR = data;
  
}


/*****************************************************************************
Name:          UART_init
Description:    Inisialize UART
Parameters:    void
Returns:        none
*****************************************************************************/
void USART_init(void)
{
  // set baud rate
  UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
  UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);
  // Enable receiver and transmitter; enable RX interrupt
  UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
  //asynchronous 8N1
  UCSRC = (1<<URSEL)|(3<<UCSZ0);
  
  

  }


